package org.sample.springbootreactive.service;

import java.net.URI;

import org.sample.springbootcommon.model.DateTimeRequest;
import org.sample.springbootcommon.model.DateTimeResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriComponentsBuilder;

import reactor.core.publisher.Mono;

@Service
public class DateTimeService {

	private static final String URL_DATE_TIME_SERVICE = "http://localhost:8080/mock/datetime";
	@Autowired
	private WebClient webClient;

	public Mono<DateTimeResponse> get() {

		URI url = UriComponentsBuilder.fromHttpUrl(URL_DATE_TIME_SERVICE).build().toUri();
		return webClient.get().uri(url).retrieve().bodyToMono(DateTimeResponse.class);
	}

	public Mono<DateTimeResponse> post(DateTimeRequest request) {

		URI url = UriComponentsBuilder.fromHttpUrl(URL_DATE_TIME_SERVICE).build().toUri();
		return webClient.post().uri(url).syncBody(request).retrieve().bodyToMono(DateTimeResponse.class);
	}

}
